
<?php 
require 'Model.php';
class UserModel extends Model{
	function getUserList(){
		$sql="select * from users";		
		$data=$this->conn->query($sql);
		return $data;
	}
	function delUser($uid){
		$sql="delete from users where uid=$uid";
		$result=$this->conn->query($sql);
		return $result;
	}
	function adduser($data,$file){
		//文件上传
		$arr=explode('.',$file['pic']['name']);
		$suffix=$arry[count($arr)-1];//后缀名
		$newname=date("YmdHis").rand(100,999).'.'.$suffix;//文件新名字
		$filepath='./photos/';//路径，将照片放在这里
		move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);
		//把数据插入数据库
		$uname=$data['username'];
		$password=$data['password'];
		$gender=$data['gender'];
		$birthdate=$data['birthdate'];
		$hobby=implode($data['hobby'], ',');
		$degree=$data['degree'];
		$intro=$data['intro'];
		$pic=$newname;
		$sql="insert into users values(null,'$uname',md5('$password'),$gender,'$birthdate','$hobby',$degree,'$intro','$newname')";//不带‘’是整形的数据
		$result=$this->conn->query($sql);
		return $result;

	}
	function edituser($uid){
		$sql="select * from users where uid=$uid";
		$data=$this->conn->query($sql);
		return $data;//一条数据

	}
	function updateuser($data,$file){
        $uid=$data['uid'];
        $username=$data['username'];
        $gender=$data['gender'];
        $birthdate=$data['birthdate'];
        $hobby=implode($data['hobby'],',');
        $degree=$data['degree'];
        $intro=$data['intro'];
		//接收数据
		if($file['pic']['size']==0){
			//没上传新头像
			$sql="update users set uname='$username',birthdate='$birthdate',gender=$gender,hobby='$hobby',degree=$degree,intro='$intro' where uid=$uid ";
		}
		else{
        $arr=explode('.',$file['pic']['name']);
		$suffix=$arry[count($arr)-1];//后缀名
		$newname=date("YmdHis").rand(100,999).'.'.$suffix;//文件新名字
		$filepath='./photos/';		move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);
		$sql="update users set uname='$username',birthdate='$birthdate',gender=$gender,hobby='$hobby',degree=$degree,intro='$intro',pic='$newname' where uid=$uid ";
			//上传头像并且根据uid更新数据
		}
		
//如果修改了头像，需要上传新头像，否则原头像不动
       $result=$this->conn->query($sql);
		return $result;
		
	}
	function searchuser($uname){
	$key=trim($_POST['key']);
    $sql="select * from users  where (uname like'%$key%')";
    $data=$this->conn->query($sql);
	return $data;
   }

}

 ?>	
